package com.dff.jrfw.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import cn.com.infosec.mobile.android.managers.CertManager;
import cn.com.infosec.mobile.android.managers.SignManager;
import cn.com.infosec.mobile.android.result.Result;

public class CertUtils {

    public static String getCertDn(Context context, String userName) {
        CertManager certManager = new CertManager(context);
        String certDn = certManager.getCertField(userName, 0);
        return certDn;
    }


    public static void sign(Context context, int which, String userName, String PIN, String plain_text, OnSignCallBackListener onSignCallBackListener) {
        SignManager signManager = new SignManager(context);
        Result.ResultListener resultListener = new Result.ResultListener() {
            @Override
            public void handleResult(Result result) {
                if (TextUtils.equals(Result.OPERATION_SUCCEED, result.getResultID())) {
                    onSignCallBackListener.onSignCallBack(result.getResultDesc());
                } else {
                    // 签名失败提示
                    Toast.makeText(context, result.toString(), Toast.LENGTH_SHORT).show();
                    onSignCallBackListener.onSignCallBack(null);
                }
            }
        };
        switch (which) {
            case 0:
                signManager.sign(userName, PIN, plain_text.getBytes(), 0, resultListener);
                break;
            case 1:
                signManager.sign(userName, PIN, plain_text.getBytes(), 1, resultListener);
                break;
            case 2:
                signManager.sign(userName, PIN, plain_text.getBytes(), 2, resultListener);
                break;
            default:
                break;
        }
    }

    public interface OnSignCallBackListener {
        void onSignCallBack(String sign);
    }

    private OnSignCallBackListener onSignCallBackListener;

    public void setOnSignCallBackListener(OnSignCallBackListener onSignCallBackListener) {
        this.onSignCallBackListener = onSignCallBackListener;
    }

}
